// pages/addcreateContractList/addcreateContractList.js

//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  khmcinput:'',
	  khmcList:[],
	  khmcId:[],
    
	qydwList:[],
	qydwId:'',
	index:0,
	htlxList:[],
	htlxId:'',
	index1:0,
	
	sfkfp:['否','是'],
	index2:0,
	
    client:[],
    height: 0,
	
	loginName: app.globalData.loginName
  },
	aKhmc: function (e) {
	  console.log(e);
		console.log(e.target.dataset.id);
	  this.setData({
	    khmcinput: e.target.dataset.text,
	    khmcId: e.target.dataset.id,
	  })
	},
	// 客户名称模糊搜索
	changeSearch: function (event) {
	  console.log(event.detail);
	  let that = this;
	  var inputSearch = event.detail.value;
	  that.getKhmc(inputSearch);
	},
  bindPickerChange: function(e) {
	console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChange1: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index1: e.detail.value
    })
  },
  bindPickerChange2: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      index2: e.detail.value
    })
  },	
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  this.getKhmc();
    this.getPageInfo();
  },

  // 删除
  recall: function(e){
    console.log(e.currentTarget.dataset.id);
    var that=this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/wxdelete', //接口地址
      data: {
        loginName: app.globalData.loginName,
        id:e.currentTarget.dataset.id
      },
      success: function (res) {
        console.log( res)
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 2000
        })
        that.onLoad();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit: function (e) {
    var that = this; 
   
  
    wx.showToast({
      icon: 'loading',
      title: 'Loading',
      duration: 2000,
      mask: true
    })
  
    console.log(this.data.clientName);
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    let { loginName, clientId, contractUnit, contractType,invoice} = e.detail.value;
    if (!clientId || !contractUnit || !contractType ) {
      wx.showToast({
        title: '请填写完整信息！',
        icon: 'none',
        duration: 2000
      })
      //wx.hideToast();
      return;
    }
    wx.request({
      url: app.globalData.apiUrl + '/wx/wxcreateContractDo', //接口地址
      data: e.detail.value,
      success: function (res) {
        console.log("新增客户信息=", res)
       if(res.data.code == 0){         
         wx.showToast({
           title: '操作成功！',
           icon: 'success',
           duration: 2000		   
         })
		
       }else{
         wx.showToast({
           title: res.data.msg,
           icon: 'none',
           duration: 2000
         })
       }
        that.onLoad();
      }
    })
  },

  getPageInfo: function(){
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/wxcreateContractData', //接口地址
      data: {
        loginName: app.globalData.loginName,
        pageNumber: 1,
        pageSize: 6,
		totalCount:0,
		pageOrderName:'sortTop DESC,opAt',
		pageOrderBy:'descending',
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        // var myOldData=that.data.imageList;
		console.log(res);
        var mydata = res.data.data.list;
        that.setData({
          client: mydata,
        })
        if (mydata == null || mydata.length == 0) {
          wx.showToast({ //如果全部加载完成了也弹一个框
            title: '没有数据了',
            //image: "/images/warn.png",
            duration: 3000
          });
        }

      }
    })
    // 初始化图片列表  结束
    // 初始化 高度   
    wx.getSystemInfo({
      success: (res) => {

        that.setData({
          height: res.windowHeight
        })
      }
    })
  },
  getKhmc:function(){
  	  var that=this;
  	  wx.request({
  		  url:app.globalData.apiUrl+'/wx/wxcreateContractList',
  		  data: {
  		    loginName: app.globalData.loginName,
  			},
  		  success:function(res){
  			  console.log("数据字典客户名称=",res);
			  that.setData({
				  khmcList:res.data.data.clients,
				  qydwList:res.data.data.contractUnit,
				  qydwId:res.data.data.contractUnit[0].id,
				  htlxList:res.data.data.contractType,
				  htlxId:res.data.data.contractType[0].id,				  
			  })								
  		  }
  	  })
  },

  lower: function(){
    var that = this;
    var result = that.data.client;
    var pagenumber = parseInt(result.length / 6 + 1);
    // 加载图片列表
    if (pagenumber < 2) {
      return false;
    } else {
      wx.request({
        url: app.globalData.apiUrl + '/wx/wxcreateContractData', //接口地址
        data: {
          loginName: app.globalData.loginName,
          pageNumber: pagenumber,
          pageSize: 6,
        },
        
        success(res) {

          if (result.length == res.data.totalCount) {
            wx.showToast({ //如果全部加载完成了也弹一个框
              title: '没有数据了',
              //image: "/images/warn.png",
              duration: 300
            });
            return false;
          }

          // var myOldData=that.data.imageList;
          var mydata = res.data.data.list;
          var cont = result.concat(mydata);
          if (mydata.length == 0) {
            wx.showToast({ //如果全部加载完成了也弹一个框
              title: '没有数据了',
              //image: "/images/warn.png",
              duration: 300
            });
            return false;
          }
          if (cont.length >= 100) {
            wx.showToast({ //如果全部加载完成了也弹一个框
              title: '加载的太多了',
              icon: 'success',
              duration: 300
            });
            return false;
          } else {
            wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中” 
              title: '加载中',
              icon: 'loading',
            });
            setTimeout(() => {
              that.setData({
                client: cont,
              });
              wx.hideLoading();
            }, 1500)
          }
        }
      })
  }
  },

})